package graphics

import (
	"image"
	"image/draw"
	"testing"

	draw9 "code.google.com/p/jnj.plan9/draw"
	//"code.google.com/p/goplan9/draw"
	"code.google.com/p/jnj/cmd/util"
)

var die = util.Die

var disp *draw9.Display
var screen *draw9.Image

func drawInit() {
	if disp != nil {
		return
	}
	var err error
	disp, err = draw9.Init(nil, "test", "500x500")
	//ch := make(chan<- error)
	//disp, err = draw.Init(ch, "/plan9/font/lucsans/euro.8.font", "test", "700x600")
	die(err)
	screen = disp.ScreenImage
}

func BenchmarkDraw(b *testing.B) {
	src := image.White
	dst := image.NewRGBA(image.Rect(0,0,500,500))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		draw.Draw(dst, dst.Bounds(), src, image.ZP, draw.Over)
	}
}

func BenchmarkDrawSrc(b *testing.B) {
	src := image.White
	dst := image.NewRGBA(image.Rect(0,0,500,500))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		draw.Draw(dst, dst.Bounds(), src, image.ZP, draw.Src)
	}
}

func BenchmarkDraw9(b *testing.B) {
	drawInit()
	src := disp.White
	dst, err := disp.AllocImage(image.Rect(0,0,500,500), draw9.RGBA32, true, draw9.Black)
	die(err)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		dst.Draw(screen.Bounds(), src, nil, image.ZP)
		disp.Flush()
	}
}

func BenchmarkDraw9Src(b *testing.B) {
	drawInit()
	src := disp.White
	dst, err := disp.AllocImage(image.Rect(0,0,500,500), draw9.RGBA32, true, draw9.Black)
	die(err)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		dst.DrawOp(screen.Bounds(), src, nil, image.ZP, draw9.S)
		disp.Flush()
	}
}
